#!/bin/sh
#chkconfig: 345 86 14
#description: Startup and shutdown script for server.jar

# 用于记录pid
PIDFILE=./java.pid
# jar包位置,可以统一使用app.jar这个名字,具体项目由文件夹决定
SERVER=./server.jar

ARGS="
-Xms128m
-Xmx128m
-Xss256k
-XX:+DisableExplicitGC
-XX:+UseConcMarkSweepGC
-XX:+CMSParallelRemarkEnabled
-XX:LargePageSizeInBytes=128m
-XX:+UseFastAccessorMethods
-XX:+UseCMSInitiatingOccupancyOnly
-XX:CMSInitiatingOccupancyFraction=50
"
start()
{
	if test -e $SERVER
	then
		echo -e "Starting $SERVER"
			if nohup java $ARGS -jar $SERVER &
		then
			echo $! > $PIDFILE
			echo -e "server start OK"
		else
			echo -e "server start failed"
		fi
	else
		echo -e "Couldn't find jar"
	fi
}

status()
{
         if test -e $PIDFILE
         then
                   echo -e "Server is running, Pid is `cat $PIDFILE`"
         else
                   echo -e "Server is not running"
         fi
}
 
stop()
{
         if test -e $PIDFILE
         then
                   echo -e "Stopping server"
                   if kill `cat $PIDFILE`
                   then
                        echo -e "server stop OK"
                   else
	                    echo -e "server stop faild"
                   fi
         else
                   echo -e "No server running"
         fi
}
 
restart()
{
    echo -e "Restarting server"
    stop
    start
}
gc(){

echo -e "gc status:\n"
if
jstat -gc `cat $PIDFILE` 1000 10
then
echo -e "gc ok"
else
echo -e 'gc fail'
fi

}
case $1 in
         start)
            start
        ;;
         stop)
        	stop
        ;;
         restart)
        	restart
        ;;
         status)
        	status
        ;;
           gc)
            gc
            ;;
         *)
        	echo "Usage: $SCRIPTNAME {start|stop|restart|status|gc}" >&2
        exit 1
        ;;
esac
exit 0
